package com.qf.dao.impl;

import com.qf.dao.UserDao;

import com.qf.entity.User;
import com.qf.utils.DBUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @Author peng
 * @Date 2023/8/21 12:52
 * @Version 1.0.0
 */

public class UserDaoImpl implements UserDao {

//    登入的Dao层
    @Override
    public User loginUser(String username, String password) throws SQLException {
        String sql = "SELECT uid,uname,password,sex,address,phone FROM `user` where uname=? and password=?";
        Object[]objects={username,password};
        User user = DBUtils.DB.qr.query(sql, new BeanHandler<User>(User.class), objects);
        return user;
    }

    //    注册的Dao层
    @Override
    public int addUser( String uname, String password, String sex, String phone,String address) throws SQLException {
        String sql = "INSERT INTO `user` (uname, `password`, sex,address, phone) VALUES (?,?,?,?,?)";
        Object [] objects = {uname,password,sex,address,phone};
        int update = DBUtils.DB.qr.update(sql, objects);
        return update;
    }

    //根据用户iu查询用户信息
    @Override
    public User selectUserById(Integer uid) throws SQLException {
        String sql = "select uid,uname,username,password,sex,address,phone from user where uid=?";
        Object[] objects = {uid};
        User user = DBUtils.DB.qr.query(sql, new BeanHandler<User>(User.class), objects);
        return user;
    }

    @Override
    public int deleteUid(int uid) throws SQLException {
        String sql = "DELETE  FROM user WHERE uid = ?";
        int num = DBUtils.DB.qr.update(sql,uid);
        return num;
    }

    @Override
    public int updateUser(User user) throws SQLException {
        String sql = "update user set uname = ?,password =?,sex =?,address =?,phone = ? where uid = ?";
        Object[] objects = {user.getUname(),user.getPassword(),user.getSex(),user.getAddress(),user.getPhone(),user.getUid()};
        int num = DBUtils.DB.qr.update(sql, objects);
        return num;
    }


}
